package com.tianyu.inspection.common.model;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianyu.inspection.common.protobuf.Telemetry;
import com.tianyu.inspection.entity.Pipeline;
import com.tianyu.inspection.mapper.PipelineMapper;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.tianyu.inspection.common.DistanceUtil.getDistance;

/**********************************
 * @Author YSW
 * @Description 飞行任务模板方法
 * @Date 2021/8/10 - 11:38
 **********************************/

public class FlyTaskModel {
    /**
     * 通过任务遥测中心点 距离 该项目下管线中心点最近距离的管线 获取任务所属管线
     * @param builder
     * @param pipelineMapper
     * @param projectId
     * @return
     */
    public static Integer getPipelineId(Telemetry.DataList.Builder builder, PipelineMapper pipelineMapper,Integer projectId) {
        final List<Telemetry.Data> dataList0 = builder.getDataList();
        Telemetry.DataList.Builder filterDataListBuilder = Telemetry.DataList.newBuilder();
        dataList0.forEach(data -> {
            if ((int)data.getLongitude() > 0 || (int)data.getLatitude() > 0){
                filterDataListBuilder.addData(data);
            }
        });
        final List<Telemetry.Data> filterDataList = filterDataListBuilder.getDataList();
        final int pointSize = filterDataList.size();
        if (pointSize == 0){
            return null;
        }
        final int centerPointIndex = pointSize / 2;
        final Telemetry.Data centerPoint = filterDataList.get(centerPointIndex);
        if ((int)centerPoint.getLongitude() > 0 || (int)centerPoint.getLatitude() > 0 ){
            final List<Pipeline> pipelineList = pipelineMapper.selectList(new QueryWrapper<Pipeline>()
                    .eq("project_id", projectId).eq("state", 0));
            if (Objects.nonNull(pipelineList) && pipelineList.size() > 0){
                final Optional<Pipeline> minDistancePipeline = pipelineList.stream().filter(pipeline -> Objects.nonNull(pipeline.getCenterPoint())).min((pipeline1, pipeline2) -> {
                    final List<Double> lngLat1 = JSONArray.parseArray(pipeline1.getCenterPoint(), Double.class);
                    final List<Double> lngLat2 = JSONArray.parseArray(pipeline2.getCenterPoint(), Double.class);
                    final double distance1 = getDistance(new double[]{lngLat1.get(1), lngLat1.get(0)},
                            new double[]{centerPoint.getLatitude(), centerPoint.getLongitude()});
                    final double distance2 = getDistance(new double[]{lngLat2.get(1), lngLat2.get(0)},
                            new double[]{centerPoint.getLatitude(), centerPoint.getLongitude()});
                    return Double.compare(distance1, distance2);
                });
                if (minDistancePipeline.isPresent()){
                    return minDistancePipeline.get().getId();
                }
            }
        }
        return null;
    }
}
